package com.hhh.petadopt.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hhh.petadopt.pojo.Lost;
import com.hhh.petadopt.pojo.LostVo;
import com.hhh.petadopt.service.AdminLostService;
import com.hhh.petadopt.utils.JsonUtils;
import com.hhh.petadopt.utils.Page;

/**
 * 失物招领管理
 * @author 杜镇声声
 *
 */
@Controller
public class AdminLostcontroller {
	
	@Autowired
	private AdminLostService adminlostservice;
	
	@RequestMapping(value="/adminLost/Lostlist.html")
	public String selectpagee(LostVo Vo,Model model) {
		
		Page<Lost> page = adminlostservice.selectPage(Vo);
		
		//将页面信息存储到域中
		model.addAttribute("page", page);
		//总条数
		model.addAttribute("total", page.getTotal());
		
		//页面信息回显
		model.addAttribute("sexx", Vo.getSexx());
		model.addAttribute("adresss", Vo.getAdresss());
		model.addAttribute("datee", Vo.getDatee());
		
		//返回视图
		return "admin/lost";
	}
	//编辑页面信息回显
	@RequestMapping(value="/adminLost/findlost.html")
	@ResponseBody
	public String selectLostById(Integer id) {
		Lost selectLostById = adminlostservice.selectLostById(id);
		//将结果转换为json格式
		String objectToJson = JsonUtils.objectToJson(selectLostById);
		return objectToJson;
	}
	
	    //文件上传
		@RequestMapping(value="/adminlost/uploadimage.html")
		@ResponseBody
		public String uploadPetPic(MultipartFile imgsss) throws IllegalStateException, IOException{
			//设置图片名称
			String pname = UUID.randomUUID().toString().replace("-", "");
			//获取原本图片名称
			String originalFilename = imgsss.getOriginalFilename();
			//获取图片扩展名
			String extName = FilenameUtils.getExtension(originalFilename);
					
			//将文件存到upload文件夹下
			imgsss.transferTo(new File("D:/upload/image/"+pname+"."+extName));
			
			//将图片的名称进行拼接
			String pic = "/pic/"+pname+"."+extName;
			
			String objectToJson = JsonUtils.objectToJson(pic);
			
			//返回拼接好的完整的图片路径
			return objectToJson;
		}
		
		//添加文件上传
		@RequestMapping(value="/adminLost/uploadaddpic.html")
		@ResponseBody
		public String uploadPetPic1(MultipartFile imgs1) throws IllegalStateException, IOException{
			//设置图片名称
			String pname = UUID.randomUUID().toString().replace("-", "");
			//获取原本图片名称
			String originalFilename = imgs1.getOriginalFilename();
			//获取图片扩展名
			String extName = FilenameUtils.getExtension(originalFilename);
					
			//将文件存到upload文件夹下
			imgs1.transferTo(new File("D:/upload/image/"+pname+"."+extName));
			
			//将图片的名称进行拼接
			String pic = "/pic/"+pname+"."+extName;
			
			String objectToJson = JsonUtils.objectToJson(pic);
			
			//返回拼接好的完整的图片路径
			return objectToJson;
		}
		
		//修改信息
		@RequestMapping(value="/adminLost/editLost.html")
		@ResponseBody
		public String dfite(Lost lost) {
			//执行修改
			adminlostservice.updateLost(lost);
			return "ok";
		}
		//添加失物招领
		@RequestMapping(value="/adminLost/addlost.html")
		@ResponseBody
		public String addLostt(Lost lost) {
			adminlostservice.addLost(lost);
			return "ok";
		}
		//根据id删除
		@RequestMapping(value="/adminlost/deletelost.html")
		@ResponseBody
		public String deleteLost(Integer id) {
			adminlostservice.deleteLostById(id);
			return "ok";
		}
		//删除多选
		@RequestMapping(value="/adminlost/deletelostttt.html")
		@ResponseBody
		public String deleteLosts(Integer[] ids) {
			adminlostservice.deleteLostsByIds(ids);
			return "ok";
		}

}
